// 添加一个 axios 请求拦截器,也可以直接使用自动生成的 OpenAPI.ts配置
import axios from "axios";

axios.defaults.baseURL = "http://localhost:8001";
axios.defaults.withCredentials = true;
axios.interceptors.request.use(
  function (config) {
    // Do something before request is sent
    // 从 localStorage 获取 token
    const token = localStorage.getItem("token");
    if (token) {
      // 如果 token 存在，将其添加到请求头
      config.headers["Authorization"] = `Bearer ${token}`;
    }
    return config;
  },
  function (error) {
    // Do something with request error
    return Promise.reject(error);
  }
);

// Add a response interceptor
axios.interceptors.response.use(
  function (response) {
    console.log("请求路径：", response.config.url);
    console.log("响应数据：", response);
    // Any status code that lie within the range of 2xx cause this function to trigger
    // Do something with response data
    return response;
  },
  function (error) {
    // Any status codes that falls outside the range of 2xx cause this function to trigger
    // Do something with response error
    return Promise.reject(error);
  }
);
